/*
 * CanvasList.java
 *
 * Created on 4 de Junho de 2007, 20:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view.components;

import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * Esta classe implementa um ListGroup usando Canvas
 * @author $Leandro$
 */
public class CanvasListGroup extends CanvasAbstract {
    
    public Vector items = null;
    
    public CanvasListItem selectedItem;
    
    public int currentX;
    
    public int currentY;
    
    public int selectionSize = 0;
    
    public int selectedIndex = 0;
    
    private int hGap = 20;
    
    private int vGap = 20;
    
    public boolean isPainted = false;
    
    public boolean isVertical;
    
    private int colorArrowUp = 0xF8A726;
    
    private int colorArrowDown = 0xF8A726;
    
    private int initPrint = 0;
    
    private int offSetPrint = 0;
    
    private int maxPrint = 0;
    
    public CanvasListGroup(Graphics g, int x, int y, boolean isVertical) {
        super(g);
        
        this.isVertical = isVertical;
        
        this.x = x;
        this.y = y;
    }
    
    public int getSize() {
        if(items == null) {
            return 0;
        } else {
            return items.size();
        }
    }
    
    public boolean isEmpty() {
        return items == null;
    }
    
    public void addItem(CanvasListItem item){
        if(selectionSize < Font.getDefaultFont().stringWidth(item.text) + item.getImgWidth()) {
            selectionSize = Font.getDefaultFont().stringWidth(item.text)  + item.getImgWidth();
        }
        
        if(hGap < item.getImgWidth()) {
            hGap = item.getImgWidth();
        }
        
        if(vGap < item.getImgHeight()) {
            vGap = item.getImgHeight();
        }
        
        if(items == null) {
            items = new Vector();
        }
        
        items.addElement(item);
    }
    
    public void paint() {
        int posY = y;
        int posX = x;
        
        int avaibleHeight = height - y;
        int avaibleWidth = width - x;
        
        int currentGap;
        
        if(isVertical) {
            currentGap = vGap;
        } else {
            currentGap = hGap;
        }
        
        if(isVertical) {
            maxPrint = avaibleWidth / vGap;
        } else {
            maxPrint = avaibleWidth / hGap;
        }
        
        setSelected();
        
        int index = 0;
        
        if(maxPrint > items.size()) {
            maxPrint = items.size();
        }
        
        for(int i = initPrint; i < maxPrint + offSetPrint && i < items.size(); i++) {
            CanvasListItem listItem = (CanvasListItem) items.elementAt(i);
            listItem.g = this.g;
            //listItem.align = g.TOP|g.LEFT;
            
            if(isVertical) {
                posY = (index)*vGap + y + 10;
                
                listItem.x = width/2;
                listItem.y = posY ;
            } else {
                posX = (index)*hGap + x;
                
                listItem.x = posX;
                listItem.y = y ;
            }
            index++;
            
            listItem.paint();
        }
        
        if(isVertical) {
            if(initPrint != 0) {
                drawArrowUp(colorArrowUp);
            }
            
            if(maxPrint-1 + offSetPrint < items.size()-1) {
                drawArrowDown(colorArrowDown);
            }
        }
    }
    
    private void drawArrowUp(int color) {
        g.setColor(color);
        g.fillTriangle(width/2,y,width/2 - 10,y + 10,width/2 + 10,y + 10);
    }
    
    private void drawArrowDown(int color) {
        g.setColor(color);
        g.fillTriangle(width/2,height,width/2 - 8,height - 8,width/2 + 8,height - 8);
    }
    
    private void setSelected() {
        if(items != null) {
            if(selectedItem != null) {
                selectedItem.isSelected = false;
                selectedItem.font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
                selectedItem.textColor.rgb = 0x000000;
            }
            selectedItem = (CanvasListItem)items.elementAt(selectedIndex);
            selectedItem.font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
            selectedItem.textColor.rgb = 0xE79208;
            selectedItem.isSelected = true;
        }
    }
    
    private void moveSelection(int xSel, int ySel,int gap) {
        g.drawRect(xSel,ySel,selectionSize,gap);
    }
    
    public void keyPressedList(String key) {
        if(items != null) {
            if(isVertical) {
                if(key.equals("UP")) {
                    colorArrowUp = 0xFAF478;
                    
                    if(selectedIndex == initPrint && initPrint != 0) {
                        offSetPrint--;
                        initPrint--;
                    }
                    
                    if(selectedIndex > 0) {
                        currentY = --selectedIndex*vGap;
                    }
                    
                } else if(key.equals("DOWN")) {
                    colorArrowDown = 0xFAF478;
                    
                    if(selectedIndex != items.size()-1 && selectedIndex == initPrint + offSetPrint) {
                        offSetPrint++;
                        initPrint++;
                    }
                    
                    if(selectedIndex < items.size()-1) {
                        currentY = ++selectedIndex*vGap;
                    }
                }
            } else {
                if(key.equals("LEFT")) {
                    colorArrowUp = 0xFAF478;
                    if(selectedIndex > 0) {
                        currentX = --selectedIndex*hGap;
                    }
                } else if(key.equals("RIGHT")) {
                    colorArrowDown = 0xFAF478;
                    if(selectedIndex < items.size()-1) {
                        currentX = ++selectedIndex*hGap;
                    }
                }
            }
            setSelected();
        }
    }
    
    public void keyReleasedList(String key) {
        if(isVertical) {
            if(key.equals("UP")) {
                colorArrowUp = 0xF8A726;
            } else if(key.equals("DOWN")) {
                colorArrowDown = 0xF8A726;
            }
        } else {
            if(key.equals("LEFT")) {
                colorArrowUp = 0xF8A726;
            } else if(key.equals("RIGHT")) {
                colorArrowDown = 0xF8A726;
            }
        }
        setSelected();
    }
}